/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import com.jnetx.javax.sip.util.ByteArrayUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SubscriptionStateHeaderParser extends ParametrizedHeaderParserBase<SubscriptionStateHeaderImpl> {
    private final static Log logger = LogFactory.getLog(SubscriptionStateHeaderParser.class);

    protected Log getLogger() {
        return logger;
    }

    public SubscriptionStateHeaderImpl createLazily(byte[] data, int start, int end) {
        return null;
    }

    //Subscription-State: active;expires=60
    public SubscriptionStateHeaderImpl[] parse(SubscriptionStateHeaderImpl header, byte[] data, int start, int end) throws ParseException {
        if (header == null) header = new SubscriptionStateHeaderImpl();
        start = ByteArrayUtil.firstMissing(data, ' ', start, end);
        if (start == -1)
            fail("Cannot parse the header!", start);
        int startOfParameter = ByteArrayUtil.firstOccurence(data, ';', start, end);
        final int endOfPart = startOfParameter != -1 ? startOfParameter : end;
        int endOfData = ByteArrayUtil.lastMissing(data, ' ', start, endOfPart);
        if (endOfData == -1)
            throw new ParseException("Cannot parse the Subscription-State header!", start);
        else
            endOfData++;
        header.setState(new String(data, start, endOfData - start));
        if (startOfParameter != -1)
            parseParameters(header, data, startOfParameter, end);
        return new SubscriptionStateHeaderImpl[]{header};
    }
}
/*
 * $Log$
 */